$(function () {
    layui.use('form', function(){
        var form = layui.form;
        //监听提交
        form.on('submit(formDemo)', function(data){
            layer.msg(JSON.stringify(data.field));
            return false;
        });
    });
    //加载供应商下拉框
    showAllSup();
    //加载登录用户
    showLoginUser();
})


layui.use('layer', function(){
    var layer = layui.layer;
});
//添加弹出层
function showAdd() {

    layer.open({
        title:"添加采购商品", //标题
        type:1,//弹出层类别，1表示页面层
        shadeClose:true,//点击遮罩关闭
        skin:'layui-layer-rim',//加上边框
        area:['620px','475px'],//宽高
        anim:1,
        content:$("#formDiv")
    });

    var supId=$("#sup").val()
    $("#sup1").val(supId)

    // $("#img").src = "/html/img/1.jpg"
}

//下拉框显示所有供应商，同时显示第一个供应商的采购栏
function showAllSup() {
    $.post("/sup/findAll",null,function (supList) {
        var str = "";
        for(var i = 0; i< supList.length;i++){
            var obj = supList[i];
            str += `<option value="${obj.id}">${obj.supName}</option>`
        }
        $("#sup").html(str)
        showBySup()
    })
}

//按照供应商下拉框显示采购物品
function showBySup() {
    var supId=$("#sup").val()
    // alert(supId)
    $.post("/sup/findBySup","&supId="+supId,function (list) {
        var str = "";
        for(var i = 0 ; i <list.length;i++){
            var obj = list[i]
            str += `<tr><td>${obj.product.name}</td>
                            <td>${obj.product.vender}</td>
                            <td>${obj.product.buyPrice}</td>
                            <td>${obj.product.sellPrice}</td>
                            <td><input type="text" value="${obj.num}"></td>
                            <td><a class="layui-btn layui-btn-danger" href="javascript:del(${obj.product.id},${obj.sup.id})">移除商品</a></td></tr>`
        }
        $("#productData").html(str)
    })
}
//按商品id删除商品
function del(productId,supId) {
 $.post("/shop/remove","&productId="+productId+"&supId="+supId,function (info) {
     if(info == "ok"){
         showBySup();
     }
 })
}


function showLoginUser() {
    $.post("/user/show",null,function (userObj) {
        $("#user").html(userObj.name)
    })
}


function addProduct() {
    var formData = new FormData($("#f1")[0]);
    $.ajax({
        url: '/shop/add' ,
        type: 'POST',
        data: formData,
        dataType:"json",
        contentType: false,
        processData: false,
        success: function (shopProductObj) {
            showBySup();
        }f
        
    });
$("#f1")[0].reset();
layer.closeAll();
}

//预览图片
function upLoad(Obj){
    var file = Obj.files[0];
    var reader = new  FileReader();
    reader.readAsDataURL(file);
    reader.onload = function (ev) {
        $("#picture").attr("src", ev.target.result);
    }
}

//自动更新弹出框
function renderForm() {
    layui.use('form', function() {
        var form = layui.form; //高版本建议把括号去掉，有的低版本，需要加()
        form.render();
    });
}
// 当添加框的编号栏失去焦点，查询是否有该商品并显示在弹出框中
function abc() {
    var proId = $("#proId").val();
    // var supId = $("#sup").val();
    $.post("/product/onblur","&proId="+proId,function (shopProduct) {
        if(shopProduct.length == 0){
        }else {
            $("#proName").val(shopProduct.product.name)
            $("#vender").val(shopProduct.product.vender)
            $("#buyPrice").val(shopProduct.product.buyPrice)
            $("#sellPrice").val(shopProduct.product.sellPrice)
            // $("#num").val(shopProduct.num)
            let type = shopProduct.product.type.toString()
            alert(type)
            $("#type").find("option:contains('"+type+"')").attr("selected",true)
            renderForm()
            $("#picture").attr("src","../img/"+shopProduct.product.photo.toString())

        }
    })
}

//商品入库
function buy() {
    var supId = $("#sup").val();
    $.post("/shop/buy","&supId="+supId,function (info) {
        if(info == "ok"){
            alert("入库成功")
            showBySup();
        }
    })
}
